package cn.glinks.liuhe.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;
import cn.glinks.liuhe.R;
import cn.glinks.liuhe.api.ApiConstants;
import cn.glinks.liuhe.api.ApiRequest;
import cn.glinks.liuhe.api.jsonbean.UserInfo;
import cn.glinks.liuhe.app.AppApplication;
import cn.glinks.liuhe.app.AppManager;
import cn.glinks.liuhe.base.BaseActivity;
import cn.glinks.liuhe.baserx.RxSubscriber;
import cn.glinks.liuhe.utils.ActTransAnimUtils;
import cn.glinks.liuhe.utils.GsonUtils;
import cn.glinks.liuhe.utils.TimeTransformUtils;
import cn.glinks.liuhe.utils.ToastUtils;
import cn.glinks.liuhe.widget.iOSDialog;

public class StartTestActivity extends BaseActivity {

    @BindView(R.id.iv_history_group)
    ImageView mHistoryGroupImageView;

    @BindView(R.id.tv_note_1)
    TextView mPagerOneNote1Text;

    @BindView(R.id.tv_note_2)
    TextView mPagerOneNote2Text;

    @BindView(R.id.tv_note_3)
    TextView mPagerOneNote3Text;

    @BindView(R.id.btn_start)
    Button mStartButton;

    @BindView(R.id.btn_statistics)
    Button mStatisticsButton;

    // 变量
    private UserInfo mUserInfo;
    private Date mStartDate;// 测评开始时间
    private Date mEndDate;// 测评开始时间

    // 常量
    private final static String BTN_START_STR = "开始测评";
    private final static String BTN_NO_NEED_STR = "测评已完成";
    private final static String BTN_EXIT_STR = "等待测评";

    @Override
    public int getLayoutId() {
        return R.layout.activity_start_test;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        // 加载用户信息
        mUserInfo = ((AppApplication) getApplicationContext()).getLoginUser();
        // 设置为透明状态栏
        SetTranslanteBar();
        // 初始化页面
        initPager();
    }

    /**
     * Des:初始化开始测评页面
     */
    private void initPager() {
        // 获取开始时间,结束时间,当前时间
        mStartDate = TimeTransformUtils.strToDate("yyyy-MM-dd HH:mm:ss",
                mUserInfo.getTestTask().getBeiginTime());
        mEndDate = TimeTransformUtils.strToDate("yyyy-MM-dd HH:mm:ss",
                mUserInfo.getTestTask().getEndTime());
        Date now = TimeTransformUtils.getNowDate();
        // 设置时间提示文本
        String text1 = TimeTransformUtils.dateToStr("HH:mm", mStartDate);
        mPagerOneNote1Text.setText(text1 + "开始测评");
        String text2 = TimeTransformUtils.dateToStr("yyyy-MM-dd", mStartDate);
        mPagerOneNote2Text.setText(text2);
        String text3 = TimeTransformUtils.dateToStr("yyyy-MM-dd HH:mm", mEndDate);
        mPagerOneNote3Text.setText(text3 + " 结束");
        // 判断是否在评测时间内和测评是否已完成
        if (now.after(mStartDate) && now.before(mEndDate)) {//是否在评测时间内
        //if (true) {
            if (mUserInfo.getTestTask().isFinished()) {//测评是否已完成
            //if (false) {
                mStartButton.setText(BTN_NO_NEED_STR);
            } else {
                mStartButton.setText(BTN_START_STR);
            }
        } else {
            mStartButton.setText(BTN_START_STR);
        }
        // 判断是否为管理员权限(1-普通用户, 2-管理员)
        if (mUserInfo.getUser().getPermission().equals("2")) {
            mStatisticsButton.setVisibility(View.VISIBLE);
        }
        // 判断是否为超级管理员权限(1-普通用户, 2-管理员, 3-超级管理员)
        if (mUserInfo.getUser().getPermission().equals("3")) {
            mStatisticsButton.setVisibility(View.VISIBLE);
            mHistoryGroupImageView.setVisibility(View.VISIBLE);
        }
    }

    /*--------------------------------------------------------------------------------
     *                          点击事件接口实现
     ---------------------------------------------------------------------------------*/
    // 测评
    @OnClick(R.id.btn_start)
    public void clickStartTest() {
        if (mStartButton.getText().equals(BTN_START_STR)) {// 开始测评
            // 发送网络请求
            mRxManager.add(ApiRequest.requestGetTestTimeStatus(mUserInfo.getJwt(),
                    mUserInfo.getTestTask().getTestTaskId()+"",
                    mUserInfo.getTestTask().getBeiginTime(),
                    mUserInfo.getTestTask().getEndTime())
                    .subscribe(new RxSubscriber<String>(mContext) {
                        @Override
                        protected void _onNext(String msg) {
                            int errorCode = GsonUtils.getIntValue(msg, "error");
                            String reason = GsonUtils.getValue(msg, "reason");
                            if (errorCode == 0) {
                                // 跳转到测评提示页面
                                ActTransAnimUtils.startActionSlideRightToLeft(StartTestActivity.this, TestPromptActivity.class);
                                finish();
                            } else if (errorCode == 3) {
                                iOSDialog.showSelectDialog(StartTestActivity.this, "提示", reason,
                                        new iOSDialog.DialogClickListener() {
                                            @Override
                                            public void confirm() {
                                                AppManager.getAppManager().AppExit(StartTestActivity.this, false);
                                            }

                                            @Override
                                            public void cancel() {

                                            }
                                        });
                            } else {
                                ApiConstants.showError(reason);
                            }
                        }

                        @Override
                        protected void _onError(String message) {
                            // TODO:处理失败响应
                            ToastUtils.showShort("获取测试时间状态失败:" + message);
                        }
                    }));

        } else if (mStartButton.getText().equals(BTN_NO_NEED_STR)) {// 测评已完成,退出测评软件
            iOSDialog.showSelectDialog(StartTestActivity.this, "提示", "测评已完成, 是否退出?",
                    new iOSDialog.DialogClickListener() {
                        @Override
                        public void confirm() {
                            AppManager.getAppManager().AppExit(StartTestActivity.this, false);
                        }

                        @Override
                        public void cancel() {

                        }
                    });
        } else if (mStartButton.getText().equals(BTN_EXIT_STR)) {// 测评时间未到,退出测评软件
            iOSDialog.showSelectDialog(StartTestActivity.this, "提示", "测评时间未到, 是否退出?",
                    new iOSDialog.DialogClickListener() {
                        @Override
                        public void confirm() {
                            AppManager.getAppManager().AppExit(StartTestActivity.this, false);
                        }

                        @Override
                        public void cancel() {

                        }
                    });
        }
    }

    // 查看统计
    @OnClick(R.id.btn_statistics)
    public void clickStatistics() {
      ActTransAnimUtils.startActionSlideRightToLeft(StartTestActivity.this, StatisticActivity.class);
    }

    // 查看历史测评
    @OnClick(R.id.iv_history)
    public void clickHistory() {
        // 跳转到历史测评页面
        ActTransAnimUtils.startActionSlideRightToLeft(StartTestActivity.this, HistoryActivity.class);
    }

    // 查看整体测评报告
    @OnClick(R.id.iv_history_group)
    public void clickHistoryAll() {
        // 跳转到整体测评报告页面
        ActTransAnimUtils.startActionSlideRightToLeft(StartTestActivity.this, GroupHistoryActivity.class);
    }


}
